package se.webbzon.boltzmann.input;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import se.webbzon.boltzmann.debug.Debug;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.resourceset.ResourceSetLoaderException;
import se.webbzon.oschi01.textureset.Texture3D;
import se.webbzon.oschi01.textureset.Texture3DSetLoader;

public class KeyboardButtonInfo implements ButtonInfo {
	
	// The font used for this keyboard button info texture
	private static final Font font = new Font("Lucida Console",Font.PLAIN,16);
	
	// The background of this keyboard button info texture
	private static final ResourceSet<Texture3D> background = Resources.getTextureSet("keyboard_key");
	
	// The name of this key
	private final String name;
	
	// The texture set associated with this key
	private final ResourceSet<Texture3D> textureSet;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public KeyboardButtonInfo(int keyCode) {
		name = codeToString((char) keyCode);
		
		// Create image
		BufferedImage image = new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = image.createGraphics();
		g2d.drawImage(background.get(0).getImage(), 0, 0, null);
		g2d.setFont(font);
		g2d.setColor(Color.black);
		g2d.drawString(name, 4, 20);
		
		// Create texture
		Texture3DSetLoader loader = new Texture3DSetLoader();
		try {
			loader.addFromBufferedImage(image);
		} catch (ResourceSetLoaderException e) {
			Debug.report(e);
		}
		textureSet = loader.getResourceSet();
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public String getName() {
		return name;
	}

	@Override public ResourceSet<Texture3D> getTextureSet() {
		return textureSet;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Returns the name of the key with the provided key code. **/
	private String codeToString(char keyCode) {
		switch (keyCode) {
		case 8: return "Backspace";
		case 9: return "Tab";
		case '\n':
		case 13: return "Enter";
		case 16: return "Shift";
		case 17: return "Ctrl";
		case 18: return "Alt";
		case 19: return "Pause";
		case 20: return "Caps lock";
		case 27: return "Escape";
		case 33: return "Page up";
		case 34: return "Page down";
		case 35: return "End";
		case 36: return "Home";
		case 37: return "Left arrow";
		case 38: return "Up arrow";
		case 39: return "Right arrow";
		case 40: return "Down arrow";
		case 45: return "Insert";
		case 46: return "Delete";
		case ' ': return "Space";
		default: return String.valueOf(keyCode);
		}
	}

}
